﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NigiriClub.WebUI.Utils.PagingHelper;

namespace NigiriClub.WebUI.Models.PagedCollections
{
    public class GamesModel
    {
        public List<Game> Games { get; set; }
        public PagingInfo PageInfo { get; set; }

        public GamesModel(int currentPage, Boolean isReviewed = true)
        {
            using (Entities db = new Entities())
            {
                Games = new List<Game>();

                PageInfo = new PagingInfo()
                {
                    TotalItems = db.Game.Where(m => m.IsReviewed == isReviewed)
                    .Count(u => true),
                    ItemsPerPage = 10,
                    CurrentPage = currentPage
                };

                //TODO .OrderByDescending(p => p.Date)
                foreach (var model in db.Game.OrderByDescending(p => p.Id)
                          .Where(m => m.IsReviewed == isReviewed &&
                            (m.ReviewerId != null && isReviewed || 
                            m.ReviewerId == null && !isReviewed))
                          .Skip((PageInfo.CurrentPage - 1) * PageInfo.ItemsPerPage)
                          .Take(PageInfo.ItemsPerPage))
                {
                    Games.Add(model);
                }
            }
        }
    }
}